package com.rongshu.view.home.activity;

import android.animation.Animator;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rongshu.R;
import com.rongshu.eventbus.AddAttention;
import com.rongshu.eventbus.AttentionStateChange;
import com.rongshu.eventbus.ChangeTextSize;
import com.rongshu.eventbus.CleanKeyBoard;
import com.rongshu.eventbus.HeaderClick;
import com.rongshu.eventbus.PlatformToShare;
import com.rongshu.eventbus.PostComment;
import com.rongshu.eventbus.ReceiverScreenEvent;
import com.rongshu.eventbus.ScrollToCommentDistrict;
import com.rongshu.eventbus.SetInput;
import com.rongshu.eventbus.UpdateCommmentCount;
import com.rongshu.utils.AnimationUtil;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.LoginUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.discovery.bean.ArticleDetaisRequest;
import com.rongshu.view.discovery.bean.AuthorDetailBean;
import com.rongshu.view.home.adapter.pageradapter.ArticleDetailsPagerAdapter;
import com.rongshu.view.home.contract.SingleArticleDetailsView;
import com.rongshu.view.home.entity.ArticleAttribute;
import com.rongshu.view.home.entity.ArticleItem;
import com.rongshu.view.home.entity.ArticleLike;
import com.rongshu.view.home.entity.UserCollect;
import com.rongshu.view.home.fragment.dialog.InputNoteDialog;
import com.rongshu.view.home.fragment.dialog.MultifunctionDialog;
import com.rongshu.view.home.presenter.SingleArticleDetailsPresenter;
import com.rongshu.view.home.widget.FontSizeView;
import com.rongshu.view.home.widget.favor.FavorLayout;
import com.rongshu.view.penpal.activity.PenpalDetailActivity;
import com.rongshu.view.userInfo.activity.ToReportActivity;
import com.umeng.socialize.UMShareAPI;
import com.utils.DensityUtil;
import com.utils.FastClickUtils;
import com.utils.NetStatusUtils;
import com.utils.SPUtils;
import com.utils.constants.SPKey;
import com.widget.CircleImageView;
import com.widget.NoScrollViewPager;
import com.widget.loading.AnimatorListener;
import com.widget.titlebar.ImmersionBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;

import static com.rongshu.utils.UserInfoUtils.getUserId;

public class SingleArticleDetailsActivity extends BaseActivity<SingleArticleDetailsPresenter> implements SingleArticleDetailsView {
    @BindView(R.id.layout_parent)
    RelativeLayout layout_parent;
    @BindView(R.id.layout_top_menu)
    LinearLayout layout_top_menu;
    @BindView(R.id.layout_top_head)
    RelativeLayout layout_top_head;
    @BindView(R.id.iv_head_icon)
    CircleImageView iv_head_icon;
    @BindView(R.id.iv_vip)
    ImageView iv_vip;
    @BindView(R.id.txt_article_author)
    TextView txt_article_author;
    @BindView(R.id.layout_add_attention)
    RelativeLayout layout_add_attention;
    @BindView(R.id.line)
    View line;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.iv_like)
    ImageView iv_like;
    @BindView(R.id.txt_like)
    TextView txt_like;
    @BindView(R.id.txt_comment)
    TextView txt_comment;
    @BindView(R.id.txt_penpal_name)
    TextView txt_penpal_name;
    @BindView(R.id.in_penpal)
    TextView in_penpal;
    @BindView(R.id.viewPager)
    NoScrollViewPager viewPager;
    @BindView(R.id.layout_no_network)
    RelativeLayout layout_no_network;
    @BindView(R.id.txt_refresh_page)
    TextView txt_refresh_page;

    //intent
    private Intent intent = null;
    //ArticleDetaisRequest请求数据
    private ArticleDetaisRequest bean;
    //是否显示进笔友会
    private boolean isEnterPenpal = false;
    //文章
    private ArticleItem articleItem = null;
    //map
    private Map<String, Object> map = null;
    //toolbarHeight
    private int toolbarHeight;
    //adapter
    private ArticleDetailsPagerAdapter mArticleDetailsPagerAdapter = null;
    //list
    private List<ArticleItem> mArticleItemList = null;
    //ArticleAttribute
    private ArticleAttribute.ResponseBean attribute = null;
    //scrollY
    private int mScrollY = 0;
    //标题位置
    private int mTitleLocation = 0;
    //是否在最顶部
    private boolean isScrollTheTop = true;
    //是否已经出现了top上的头像，关注信息
    private boolean isShowAttention = false;
    //是否滑动到了评论区域
    private boolean isScrollToComment = false;
    //是否手动点赞
    private boolean isManualLike = false;
    //点赞爱心
    private FavorLayout favorLayout = null;
    //点赞提示语
    private List<String> likeTips = null;
    //request
    public static final int REQUEST_LOGIN = 0X0A;

    //dialog
    private InputNoteDialog inputNoteDialog = null;
    //内容
    private String mContent = "";
    //提示
    private String mReplyContent = null;
    //@的用戶信息
    private LinkedHashMap<String, AuthorDetailBean> authorDetailBeanMap = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hintTitleBar();
        ImmersionBar.with(this).keyboardEnable(false,
                WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)
                .init();
        EventBus.getDefault().register(this);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_article_details;
    }

    @Override
    protected void initView() {
        //presenter
        setPresenter(new SingleArticleDetailsPresenter(this), this);
        //margin
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) layout_top_menu.getLayoutParams();
        toolbarHeight = DensityUtil.getStateBar(this);
        layoutParams.height = toolbarHeight + getResources().getDimensionPixelSize(R.dimen.dd_dimen_91px);
        layout_top_menu.setLayoutParams(layoutParams);
        layout_top_menu.setPadding(0, toolbarHeight, 0, 0);
        layout_top_menu.setBackgroundColor(getResources().getColor(R.color.white));
        //list
        likeTips = Arrays.asList(new String[]{
                getString(R.string.its_nice_to_be_liked),
                getString(R.string.received_your_like_thank_you),
                getString(R.string.once_thumb_up_very_close),
                getString(R.string.warm_praise_reliable_shoulder),
                getString(R.string.thanks_for_your_encouragement)});
        //intent
        intent = getIntent();
        if (intent == null) {
            return;
        }
        bean = (ArticleDetaisRequest) intent.getSerializableExtra(Constant.ARTICLE_DETAILS_REQUEST);
        isEnterPenpal = intent.getBooleanExtra(Constant.INTENT_STATE, false);
        if (bean != null) {
            //类型转换
            this.articleItem = new ArticleItem();
            this.articleItem.setContribute_id(bean.getContribute_id());
            this.articleItem.setTopic_id(bean.getTopic_id());
            this.articleItem.setType(bean.getType());
            this.articleItem.setSort(bean.getChapter());
            this.articleItem.setNextChapter(bean.getSerialize_flip_over_type());
        }
        //map
        authorDetailBeanMap = new LinkedHashMap<>();
        //adapter
        mArticleDetailsPagerAdapter = new ArticleDetailsPagerAdapter(getSupportFragmentManager());
        //pager
        viewPager.setAdapter(mArticleDetailsPagerAdapter);
        viewPager.setNoScroll(true);
        viewPager.setCurrentItem(0);
        //爱心
        favorLayout = new FavorLayout(this);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                getResources().getDimensionPixelOffset(R.dimen.dd_dimen_700px));
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        lp.bottomMargin = getResources().getDimensionPixelOffset(R.dimen.dd_dimen_32px);
        favorLayout.setLayoutParams(lp);
        layout_parent.addView(favorLayout);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        //请求单个文章或则连载翻页
        if (articleItem == null) {
            return;
        }
        if (articleItem.getNextChapter() == 0) {
            getOnePostDetail();
        } else {
            getFEPageTurn();
        }
    }

    /**
     * 单个文章详情
     */
    public void getOnePostDetail() {
        if (articleItem == null) {
            return;
        }
        if (!NetStatusUtils.isConnected(this)) {
            layout_no_network.setVisibility(View.VISIBLE);
            return;
        } else {
            layout_no_network.setVisibility(View.GONE);
        }
        showLoading(true);
        map = new HashMap<>();
        map.put("user_id", getUserId());
        map.put("contribute_id", articleItem.getContribute_id());
        map.put("type", articleItem.getType());//1通过首页进入文章，2通过搜索结果点击进入文章，3通过笔友会帖子进入文章，4通过别人推荐进入文章，5通过浏览历史进入文章，6通过别人的主页进入文章
        map.put("topic_id", articleItem.getTopic_id());
        presenter.getOnePostDetail(
                (int) map.get("user_id"),
                (int) map.get("contribute_id"),
                (int) map.get("type"),
                (int) map.get("topic_id"),
                Common.encryptionSign(map));
        map.clear();
    }

    /**
     * 连载翻页
     */
    private void getFEPageTurn() {
        if (articleItem == null) {
            return;
        }
        showLoading(true);
        map = new HashMap<>();
        map.put("user_id", getUserId());
        map.put("serialize_id", articleItem.getContribute_id());
        map.put("sort", articleItem.getSort());
        map.put("type", articleItem.getNextChapter());
        presenter.getFEPageTurn(
                (int) map.get("user_id"),
                (int) map.get("serialize_id"),
                (int) map.get("sort"),
                (int) map.get("type"),
                Common.encryptionSign(map));
        map.clear();
    }

    /**
     * 获取我是否收藏
     */
    private void getIsShouChang() {
        //user_id
        int user_id = getUserId();
        if (user_id == 0) {
            return;
        }
        if (articleItem == null) {
            return;
        }

        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("contribute_id", articleItem.getContribute_id());
        map.put("type", 1);
        presenter.getIsShouChang((int) map.get("user_id"),
                (int) map.get("contribute_id"),
                (int) map.get("type"),
                Common.encryptionSign(map));
        map.clear();
    }


    /**
     * 浏览
     */
    private void getbrowse() {
        //user_id
        int user_id = getUserId();
        if (user_id == 0) {
            return;
        }
        if (articleItem == null) {
            return;
        }
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("contribute_id", articleItem.getContribute_id());
        map.put("type", 1);
        map.put("code", "browse");
        presenter.getbrowse((int) map.get("user_id"),
                (int) map.get("contribute_id"),
                (int) map.get("type"),
                (String) map.get("code"),
                Common.encryptionSign(map));
        map.clear();
    }

    /**
     * 收藏
     */
    private void collectArticle() {
        //user_id
        int user_id = getUserId();
        if (user_id == 0) {
            LoginUtils.toLogin(this, REQUEST_LOGIN);
            return;
        }
        if (articleItem == null) {
            return;
        }
        //http
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("type", 1);//(1文章 2帖子)
        map.put("content_id", articleItem.getContribute_id());
        presenter.getIsCollect(
                (int) map.get("user_id"),
                (int) map.get("type"),
                (int) map.get("content_id"),
                Common.encryptionSign(map));
        map.clear();
    }

    /**
     * 获取文章属性
     */
    private void getArticleAttribute() {
        int user_id = getUserId();
        if (articleItem == null) {
            return;
        }
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("contribute_id", articleItem.getContribute_id());
        presenter.getArticleAttribute((int) map.get("user_id"),
                (int) map.get("contribute_id"),
                Common.encryptionSign(map));
    }

    /**
     * 文章点赞/帖子点赞
     */
    public void getLike() {
        //user_id
        int user_id = getUserId();
        if (user_id == 0) {
            LoginUtils.toLogin(this, REQUEST_LOGIN);
            return;
        }
        if (articleItem == null) {
            return;
        }
        //id
        int id = articleItem.getContribute_id();
        //http
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("type", "article");//点赞类型(post帖子，article文章)
        presenter.getlike(
                id, (int) map.get("user_id"),
                (String) map.get("type"),
                Common.encryptionSign(map));
        map.clear();
    }

    /**
     * 取消文章点赞
     */
    public void getOfflike() {
        //user_id
        int user_id = getUserId();
        if (user_id == 0) {
            LoginUtils.toLogin(this, REQUEST_LOGIN);
            return;
        }
        if (articleItem == null) {
            return;
        }
        //id
        int id = articleItem.getContribute_id();
        //http
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("type", "article");//点赞类型(post帖子，article文章)
        map.put("contribute_id", id);
        presenter.getOfflike(
                (int) map.get("user_id"),
                (String) map.get("type"),
                (int) map.get("contribute_id"),
                Common.encryptionSign(map));
        map.clear();
    }

    /**
     * Toolbar背景，icon替换
     */
    private void replaceToolbarrResource(boolean isWhite) {
        if (mScrollY == 0) {
            layout_top_menu.getBackground().setAlpha(isWhite ? 255 : 0);
        } else {
            if (mScrollY <= mTitleLocation && !isWhite) {
                int alpha = (int) ((mScrollY / Float.valueOf(mTitleLocation)) * 255);
                layout_top_menu.getBackground().setAlpha(alpha);
            } else {
                layout_top_menu.getBackground().setAlpha(255);
            }
        }
        iv_back.setBackgroundResource(isWhite ? R.drawable.gray_back_icon
                : isScrollTheTop ? R.drawable.round_back_icon : R.drawable.gray_back_icon);
        line.setVisibility(isWhite ? isScrollTheTop ? View.GONE : View.VISIBLE :
                mScrollY > mTitleLocation ? View.VISIBLE : View.GONE);
        //是否显示进入笔友会
        in_penpal.setVisibility(isEnterPenpal && articleItem.getPenpalwill_id() != 0 ? View.VISIBLE : View.GONE);
        in_penpal.setBackgroundResource(isWhite ?
                R.drawable.shape_green_stroke_tran_solid :
                mScrollY > mTitleLocation ? R.drawable.shape_green_stroke_tran_solid : R.drawable.shape_black_stroke_tran_solid);
        txt_penpal_name.setVisibility(isEnterPenpal && articleItem.getPenpalwill_id() != 0 ? View.VISIBLE : View.GONE);
        txt_penpal_name.setTextColor(isWhite ?
                getResources().getColor(R.color.color_666666) :
                mScrollY > mTitleLocation ? getResources().getColor(R.color.color_666666) : getResources().getColor(R.color.white));
        txt_penpal_name.setText(articleItem.getPenclubname());
        if (!isEnterPenpal && articleItem.getPenpalwill_id() == 0) {
            //头像
            RequestOptions options = new RequestOptions()
                    .placeholder(R.drawable.user_head_icon);
            Glide.with(this)
                    .load(articleItem.getAvatar())
                    .apply(options)
                    .into(iv_head_icon);
            //vip
            iv_vip.setVisibility(articleItem.isVip() ? View.VISIBLE : View.GONE);
            //作者
            txt_article_author.setText(articleItem.getBi_name());
            //关注layout
            layout_top_head.setVisibility(isShowAttention ? View.VISIBLE : View.GONE);
            layout_add_attention.setVisibility(articleItem.getUser_id() == getUserId() ?
                    View.GONE : articleItem.getIsConcern() > 0 ?
                    View.GONE : isShowAttention ? View.VISIBLE : View.GONE);//0没有关注，1已关注
        }
    }

    /***
     *点赞数
     */
    private void setIv_like(boolean islike, int likeCount) {
        iv_like.setImageResource(islike ? R.drawable.love_red_icon : R.drawable.love_icon);
        if (islike && isManualLike) {
            isManualLike = false;
            favorLayout.addFavor();
            toast(likeTips.get(new Random().nextInt(likeTips.size())));
        }
        txt_like.setText(likeCount + "");
    }

    /**
     * 评论数
     */
    private void setCommentCount(int commentCount) {
        txt_comment.setText(commentCount + "");
    }

    /**
     * 点赞，评论
     */
    private void updateArticleAttribute() {
        if (attribute == null) {
            return;
        }
        //赞
        final boolean isLike = attribute.getIsLike() == 0 ? false : true;
        final int likeCount = attribute.getLikeCount();
        setIv_like(isLike, likeCount);
        //评论
        int commentCount = attribute.getCommentCount();
        setCommentCount(commentCount);
    }

    @OnClick({R.id.head_left, R.id.in_penpal, R.id.layout_function, R.id.txt_wrtie_note,
            R.id.layout_love, R.id.layout_comment, R.id.layout_share, R.id.layout_add_attention,
            R.id.iv_head_icon, R.id.txt_refresh_page})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.head_left:
                if (FastClickUtils.isFastClick()) {
                    return;
                }
                onBackPressed();
                break;
            case R.id.layout_function://菜单
                if (FastClickUtils.isFastClick()) {
                    return;
                }
                MultifunctionDialog.Builder builder = new MultifunctionDialog.Builder(this);
                builder.setIsCollect(articleItem.getIsCollect() == 0 ? false : true)
                        .setOnChangeCallbackListener(new FontSizeView.OnChangeCallbackListener() {
                            @Override
                            public void onChangeListener(int position) {
                                //获取是否保存过字体类型
                                final int fontType = SPUtils.getPreferences(SPKey.FONTPATTERN, 1);
                                if (fontType == position) {
                                    return;
                                }
                                SPUtils.putPreferences(SPKey.FONTPATTERN, position);
                                EventBus.getDefault().post(new ChangeTextSize(position));
                            }
                        })
                        .setOnCollectReportClickListener(new MultifunctionDialog.Builder.OnCollectReportClickListener() {
                            @Override
                            public void onCollectClick() {
                                if (FastClickUtils.isFastClick()) {
                                    return;
                                }
                                collectArticle();
                            }

                            @Override
                            public void onReportClick(Dialog dialog) {
                                if (FastClickUtils.isFastClick() && articleItem == null) {
                                    return;
                                }

                                if (getUserId() == 0) {
                                    LoginUtils.toLogin(SingleArticleDetailsActivity.this, REQUEST_LOGIN);
                                    return;
                                }
                                Intent intent = new Intent(SingleArticleDetailsActivity.this, ToReportActivity.class);
                                intent.putExtra(Constant.INTENT_MESSAGE, articleItem.getContribute_id());
                                intent.putExtra(Constant.INTENT_TYPE, 3);
                                startActivity(intent);
                                dialog.dismiss();
                            }
                        }).create().show();
                break;
            case R.id.layout_love://赞
                if (FastClickUtils.isFastClick()) {
                    return;
                }

                if (attribute == null) {
                    return;
                }

                boolean isLike = attribute.getIsLike() == 0 ? false : true;
                if (!isLike) {
                    isManualLike = true;
                    getLike();
                } else {
                    getOfflike();
                }
                break;
            case R.id.layout_comment://评论
                if (FastClickUtils.isFastClick() || attribute == null) {
                    return;
                }

                EventBus.getDefault().post(new ScrollToCommentDistrict());
                if (getUserId() != 0) {
                    int comment = attribute.getCommentCount();
                    if (comment == 0 && !isScrollToComment) {
                        getInputDialog().show(getSupportFragmentManager(), InputNoteDialog.class.getSimpleName());
                    }
                }
                break;
            case R.id.layout_share://分享
                if (FastClickUtils.isFastClick()) {
                    return;
                }
                EventBus.getDefault().post(new PlatformToShare());
                break;
            case R.id.txt_wrtie_note://写留言
                if (FastClickUtils.isFastClick()) {
                    return;
                }
                if (getUserId() == 0) {
                    LoginUtils.toLogin(this, REQUEST_LOGIN);
                    return;
                }
                getInputDialog().show(getSupportFragmentManager(), InputNoteDialog.class.getSimpleName());
                break;
            case R.id.in_penpal:
                if (FastClickUtils.isFastClick() && articleItem == null) {
                    return;
                }
                if (getUserId() == 0) {
                    LoginUtils.toLogin(this, REQUEST_LOGIN);
                    return;
                }
                intent = new Intent(this, PenpalDetailActivity.class);
                intent.putExtra(Constant.PENPAL_ID, articleItem.getPenpalwill_id());
                startActivity(intent);
                break;
            case R.id.iv_head_icon://头像
                if (FastClickUtils.isFastClick()) {
                    return;
                }
                EventBus.getDefault().post(new HeaderClick());
                break;
            case R.id.layout_add_attention://加关注
                if (FastClickUtils.isFastClick()) {
                    return;
                }
                AnimationUtil.AlphaScaleAnimator(layout_add_attention, new AnimatorListener() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        if (layout_add_attention != null) {
                            layout_add_attention.animate().alpha(1).scaleX(1).scaleY(1);
                        }
                    }
                });
                EventBus.getDefault().post(new AddAttention());
                break;
            case R.id.txt_refresh_page:
                updateViews(false);
                break;
        }
    }

    /**
     * 显示输入框弹窗
     */
    private InputNoteDialog getInputDialog() {
        inputNoteDialog = InputNoteDialog.getInstance(mContent, mReplyContent, authorDetailBeanMap);
        inputNoteDialog
                .setOnDialogInputListener(new InputNoteDialog.OnDialogInputListener() {
                    @Override
                    public void sendCommentSuccess(LinkedHashMap<String, AuthorDetailBean> map, String content) {
                        EventBus.getDefault().post(new PostComment(content, authorDetailBeanMap));
                    }

                    @Override
                    public void dimiss(LinkedHashMap<String, AuthorDetailBean> map, String content, String replyContent) {
                        inputNoteDialog.dismissDialog();
                        inputNoteDialog = null;
                        authorDetailBeanMap = map;
                        mContent = content;
                        mReplyContent = replyContent;
                    }
                });
        return inputNoteDialog;
    }

    /**
     * 单个文章详情
     *
     * @param articleItem
     */
    @Override
    public void onOnePostDetail(ArticleItem articleItem) {
        if (articleItem != null) {
            showLoading(false);
            this.articleItem = articleItem;
            //收藏
            getIsShouChang();
            //修改toolbar背景
            boolean isWhiteBg = TextUtils.isEmpty(articleItem.getTopImg()) ? true : false;
            replaceToolbarrResource(isWhiteBg);
            //adapter
            this.mArticleItemList = new ArrayList<>();
            this.mArticleItemList.add(articleItem);
            mArticleDetailsPagerAdapter.setResponseBeanList(mArticleItemList);
            mArticleDetailsPagerAdapter.notifyDataSetChanged(true);
            //获取文章属性
            getArticleAttribute();
            //统计阅读
            getbrowse();
        } else {
            updateViews(false);
        }
    }

    /**
     * 浏览
     *
     * @param bean
     */
    @Override
    public void onBrowseArticle(INetBean bean) {

    }

    /**
     * 获取是否收藏
     *
     * @param userCollect
     */
    @Override
    public void onIsConcern(UserCollect userCollect) {
        if (userCollect.getErrCode() == 0) {
            UserCollect.ResponseBean bean = userCollect.getResponse();
            this.articleItem.setIsCollect(bean.getIsShouChang());
        }
    }

    /**
     * 收藏
     *
     * @param iNetBean
     */
    @Override
    public void onIsCollect(INetBean iNetBean) {
        if (iNetBean.getErrCode() == 0) {
            if (iNetBean.getErrMsg().equals("收藏成功")) {
                this.articleItem.setIsCollect(1);
                toast("已收藏，可至个人中心查看我收藏的");
            } else {
                this.articleItem.setIsCollect(0);
                toast("取消收藏成功");
            }
        }
    }

    /**
     * 获取文章属性
     *
     * @param attribute
     */
    @Override
    public void onArticleAttribute(ArticleAttribute attribute) {
        if (attribute == null) {
            return;
        }
        ArticleAttribute.ResponseBean bean = attribute.getResponse();
        if (bean == null) {
            return;
        }
        this.attribute = bean;
        updateArticleAttribute();
    }

    /**
     * 点赞，取消点赞
     *
     * @param articleLike
     * @param isLike
     */
    @Override
    public void onArticleLikeOrOffLike(ArticleLike articleLike, boolean isLike) {
        if (articleLike != null) {
            if (articleLike.getErrCode() == 0) {
                if (attribute == null) {
                    return;
                }
                int likeCount = attribute.getLikeCount();
                likeCount = isLike ? likeCount + 1 : likeCount <= 0 ? 0 : likeCount - 1;
                attribute.setLikeCount(likeCount);
                attribute.setIsLike(isLike ? 1 : 0);
                //更新点赞
                setIv_like(isLike, likeCount);
            } else {
                toast(articleLike.getErrMsg());
            }
        }
    }

    /**
     * 屏幕单击，滑动事件处理
     *
     * @param screenEvent
     */
    @Subscribe
    public void receiveScreenEvent(ReceiverScreenEvent screenEvent) {
        mScrollY = screenEvent.getScrollY();
        int oldScrollY = screenEvent.getOldScrollY();
        mTitleLocation = screenEvent.getTitle_location();
        int head_location = screenEvent.getHead_location();
        int commentLocation = screenEvent.getCommtentLocation();
        //是否出现在最顶部
        isScrollTheTop = mScrollY == 0 ? true : false;
        //是否滑动到了评论
        isScrollToComment = mScrollY >= commentLocation;
        //是否显示关注
        isShowAttention = mScrollY > head_location;
        //修改toolbar背景
        boolean isWhiteBg = TextUtils.isEmpty(articleItem.getTopImg()) ? true : false;
        replaceToolbarrResource(isWhiteBg);
    }

    /**
     * 评论区增加，删除，更新评论数
     */
    @Subscribe
    public void updateCommentCount(UpdateCommmentCount updateCommmentCount) {
        if (updateCommmentCount == null) {
            return;
        }
        boolean isAddComment = updateCommmentCount.isAddComment();
        //如果评论增加，则评论数需要增加1
        if (attribute == null) {
            return;
        }
        int count = attribute.getCommentCount();
        count = isAddComment ? count + 1 : count <= 0 ? 0 : count - 1;
        attribute.setCommentCount(count);
        setCommentCount(count);
    }

    /**
     * 评论或则回复成功后，关闭并清空dialog输入框信息
     */
    @Subscribe
    public void cleanKeyBoard(CleanKeyBoard keyBoard) {
        if (keyBoard == null) {
            return;
        }
        inputNoteDialog.dismissDialog();
    }

    /**
     * 回复或则回复回复，设置dialog输入框提示信息
     */
    @Subscribe
    public void setInputHint(SetInput input) {
        if (input == null) {
            return;
        }
        mReplyContent = input.getReplyContent();
        getInputDialog().show(getSupportFragmentManager(), InputNoteDialog.class.getSimpleName());
    }

    /**
     * 关注状态请求
     */
    @Subscribe
    public void onAttentionStateChange(AttentionStateChange stateChange) {
        if (articleItem == null || stateChange == null && isEnterPenpal && articleItem.getPenpalwill_id() != 0) {
            return;
        }
        articleItem.setIsConcern(stateChange.getIsConcern());
        if (layout_add_attention != null) {
            layout_add_attention.setVisibility(articleItem.getUser_id() == getUserId() ?
                    View.GONE : articleItem.getIsConcern() > 0 ?
                    View.GONE : isShowAttention ? View.VISIBLE : View.GONE);//0没有关注，1已关注
        }
        if (stateChange.isOtherHomePagerReturn()) {
            getIsShouChang();
        }
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        if (attribute != null) {
            intent.putExtra(Constant.LIKE_COUNT, attribute.getLikeCount());
            intent.putExtra(Constant.COMMENT_COUNT, attribute.getCommentCount());
        }
        if (articleItem != null) {
            intent.putExtra(Constant.IS_COLLECT, articleItem.getIsCollect());
        }
        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_LOGIN://登录界面返回
                    EventBus.getDefault().post(new AddAttention());
                    //获取文章属性
                    getArticleAttribute();
                    //获取我是否收藏
                    getIsShouChang();
                    //统计阅读
                    getbrowse();
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
